package org.cvcsc.weproject.service;

import org.cvcsc.weproject.domain.AttendDetailStatus;
import org.cvcsc.weproject.domain.Attendance;
import org.cvcsc.weproject.domain.CourseSchedule;
import org.cvcsc.weproject.domain.Student;
import org.cvcsc.weproject.repository.AttendanceRepository;
import org.cvcsc.weproject.repository.CourseScheduleRepository;
import org.cvcsc.weproject.repository.Finder;
import org.cvcsc.weproject.repository.ModelFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AttendanceService {

    @Autowired
    private AttendanceRepository attendanceRepository;

    @Autowired
    private CourseScheduleRepository courseScheduleRepository;

    @Autowired
    private Finder<Student, Long> studentRepository;

    @Autowired
    private ModelFinder modelFinder;

    // ... 其他代码保持不变 ...

    public void removeAllAttendances(Long clazzId) {
        attendanceRepository.deleteByClazzId(clazzId);
    }



    public long startAttendance(Long courseScheduleId) {
        CourseSchedule courseSchedule =this.courseScheduleRepository.findById(courseScheduleId).orElseThrow(()->new RuntimeException());
        List<Student> studentsByClazzId = this.modelFinder.findStudentsByClazzId(courseSchedule.getClazz().getId());
        Attendance attendance = Attendance.instanceAndStart(courseSchedule,studentsByClazzId);
        return this.attendanceRepository.save(attendance).getId();
    }

    /**
     * 签到
     * @param attendId
     * @param studentId
     */
    public void checkAttendance(Long attendId, Long studentId) {
        Attendance attendance=this.attendanceRepository.findById(attendId).orElseThrow(()->new RuntimeException("签到表不存在") );

        Student student=this.studentRepository.findById(studentId).orElseThrow(()->new RuntimeException("学生不存在") );
        attendance.checkAttend(student);
        this.attendanceRepository.save(attendance);

    }

    /**提交考勤
     *
     * @param attendId
     * @param studentId
     */
    public void submitAttendance(Long attendId, Long studentId) {
        Attendance attendance=this.attendanceRepository.findById(attendId).orElseThrow(()->new RuntimeException("签到表不存在") );
        Student student=this.studentRepository.findById(studentId).orElseThrow(()->new RuntimeException("学生不存在") );
        attendance.submit();
        this.attendanceRepository.save(attendance);
    }
    /**锁定考勤
     *
     * @param attendId
     * @param studentId
     */
    public void lockAttendance(Long attendId, Long studentId) {
        Attendance attendance=this.attendanceRepository.findById(attendId).orElseThrow(()->new RuntimeException("签到表不存在") );
        Student student=this.studentRepository.findById(studentId).orElseThrow(()->new RuntimeException("学生不存在") );
        attendance.lock();
        this.attendanceRepository.save(attendance);
    }





    //TODO 这个方法要改 改成update
    public long tuneAttendance(Long attendId, Map<Student, AttendDetailStatus> studentStatusMap) {

        Attendance attendance = this.attendanceRepository.findById(attendId).orElseThrow(()->new RuntimeException("不存在这张考勤表"));
        attendance.tune(studentStatusMap);
        return this.attendanceRepository.save(attendance).getId();
    }

}
